<?php
require_once 'snc-core/snc-mail/PEAR/Mail.php';
include_once  'snc-core/env/Context.php';
class AbstractMail {
	public static $context;
	private $mail;
	private $headers;
	private $body;
	function __construct(){
		$this->mail = new Mail();
	}
	
	/**
	 * SMTP mail from config data 
	 */
	public function getConnectionFromConfig(){
		$mailConfig = self::$context["CONFIG"]["basic"]["mail"];
		$host = $mailConfig["host"];
		$username = $mailConfig["username"];
		$password = $mailConfig["password"];
		$port = $mailConfig["port"];
		//TODO: authentification implement		
		$config = array("host"=>$host,
					 "port"=>$port,
					 "auth"=>true,
					 "username"=>$username,
					 "password"=>$password,
					 "persit"=>false
					);
				
		return $this->mail->factory("SMTP", $config);
	
	}
	
	/**
	 * smtp mail from data outside
	 * Enter description here ...
	 * @param unknown_type $host
	 * @param unknown_type $username
	 * @param unknown_type $password
	 * @param unknown_type $port
	 * @param unknown_type $auth
	 * @param unknown_type $persit
	 */
	public function getConnection($host,$username,$password,$port,$auth,$persit){
		$config = array("host"=>$host,
					 "port"=>$port,
					 "auth"=>$auth,
					 "username"=>$username,
					 "password"=>$password,
					 "persit"=>$persit
					);
			
		return $this->mail->factory('SMTP', $config);
	}
	
	/**
	 * set Header	 
	 * @param unknown_type $from
	 * @param unknown_type $to
	 * @param unknown_type $subject
	 * @param unknown_type $contentType
	 * @param unknown_type $mimeVersion
	 */
	public function setHeader($from,$to,$subject,$contentType=null,$mimeVersion=null){
		$this->headers = array("from"=>$from,
							  "to"=>$to,
							  "Subject"=>$subject,
							  "Content-Type"=>"multipart/related",
							  "MIME-Version"=>"1.0");
	}
	
	public function setBody($body){
		$this->body = $body;
	}
	
	/**
	 * call this method with condition that header and connection existed	 
	 */
	public function send(){
		set_time_limit(15);
		$send = $this->mail->send($this->headers["to"], $this->headers, $this->body);
		
		$pear  = new PEAR();
		if($pear->isError($send)){
			return "There were something wrong. Details: ".$send->getMessage();
		}else{
			return $send;
		}
        
	}
	

}